var Common = require('RoleCommon');

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        _actionStatus: Common.ActionStatus.STAND,
        _speedX: 8,
        _speedY: 4,
        _isDead: false,
        _isAttacking: false,
        _commonAttackNum: 5,
        _commonAttackCD: 50,
        _commonAttackStatus: Common.AttackStatus.ATTACK_1,
        _commonAttackCDTemp: 0,
        _angle: 0,
    },

    // use this for initialization
    onLoad: function () {
        this.anim = this.getComponent(cc.Animation);
        this.anim.on('finished', this.onAnimFinished, this);
    },
    stand: function () {
        if (this._actionStatus != Common.ActionStatus.STAND) {
            this.anim.play("stand");
            this._actionStatus = Common.ActionStatus.STAND;
        }
    },
    hurt: function () {
        this.anim.play("hurt");
    },
    dead: function () {
        this.anim.play("dead");
    },
    moveByAngle: function (angle) {
        if (this._isDead) { return; }
        if (this._isAttacking) { return; }

        this._angle = angle;

        if (this._actionStatus != Common.ActionStatus.WALK) {
            this.anim.play("walk");
            this._actionStatus = Common.ActionStatus.WALK;
        }
    },
    moveStop: function () {
        //cc.log("moveStop");
        this._angle = -999;
    },
    commonAttack: function () {
        if (this._isAttacking) {
            return;
        }
        this._isAttacking = true;
        //cc.log("_actionStatus", this._actionStatus, this._commonAttackStatus);
        if (this._commonAttackStatus == Common.AttackStatus.ATTACK_1) {
            this.anim.play("attack1");
        } else if (this._commonAttackStatus == Common.AttackStatus.ATTACK_2) {
            this.anim.play("attack2");
        } else if (this._commonAttackStatus == Common.AttackStatus.ATTACK_3) {
            this.anim.play("attack3");
        } else if (this._commonAttackStatus == Common.AttackStatus.ATTACK_4) {
            this.anim.play("attack4");
        } else if (this._commonAttackStatus == Common.AttackStatus.ATTACK_5) {
            this.anim.play("attack5");
        }
        this._actionStatus = Common.ActionStatus.ATTACK;
    },
    onAnimFinishedPre: function (name) {
        this._commonAttackCDTemp = this._commonAttackCD;
        if (name == "attack1") {
            if (this._commonAttackNum == this._commonAttackStatus) {
                this._commonAttackStatus = Common.AttackStatus.ATTACK_1;
            } else {
                this._commonAttackStatus = Common.AttackStatus.ATTACK_2;
            }
        } else if (name == "attack2") {
            if (this._commonAttackNum == this._commonAttackStatus) {
                this._commonAttackStatus = Common.AttackStatus.ATTACK_1;
            } else {
                this._commonAttackStatus = Common.AttackStatus.ATTACK_3;
            }
        } else if (name == "attack3") {
            if (this._commonAttackNum == this._commonAttackStatus) {
                this._commonAttackStatus = Common.AttackStatus.ATTACK_1;
            } else {
                this._commonAttackStatus = Common.AttackStatus.ATTACK_4;
            }
        } else if (name == "attack4") {
            if (this._commonAttackNum == this._commonAttackStatus) {
                this._commonAttackStatus = Common.AttackStatus.ATTACK_1;
            } else {
                this._commonAttackStatus = Common.AttackStatus.ATTACK_5;
            }
        } else if (name == "attack5") {
            this._commonAttackStatus = Common.AttackStatus.ATTACK_1;
        }
    },
    onAnimFinished: function (event) {
        //cc.log(event);
        var animationState = event.detail;
        var name = animationState.name;
        if (name == "attack1" || name == "attack2" || name == "attack3" || name == "attack4" || name == "attack5") {
            this._isAttacking = false;

            PigeonJs.emit("msg.commonAttack", { name: name });
        }
    },
    onHold: function () {
        if (this._isAttacking == false && this._commonAttackCDTemp <= 0 && this._angle == -999) {
            if (this._actionStatus != Common.ActionStatus.STAND) {
                this.anim.play("stand");
                this._actionStatus = Common.ActionStatus.STAND;
            }
        }
    },
    callPerFrame: function (dt) {
        if (this._isDead) {
            this.dead();
            return;
        }

        //移动
        if (this._actionStatus == Common.ActionStatus.WALK) {
            if (this._angle >= -180 && this._angle <= 180) {
                if (this._angle > -90 && this._angle <= 90) {
                    this.node.scaleX = -1;
                } else {
                    this.node.scaleX = 1;
                }
                this.node.x += Math.cos(this._angle * (Math.PI / 180)) * this._speedX;
                this.node.y += Math.sin(this._angle * (Math.PI / 180)) * this._speedY;
            }
        }

        //普通攻击连击cd
        if (this._commonAttackCDTemp > 0) {
            if (this._commonAttackCDTemp == 1) {
                this._commonAttackStatus = Common.AttackStatus.ATTACK_1;
            }
            this._commonAttackCDTemp--;
        }

        this.onHold();
    },
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
